﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;
using System.Linq;

namespace TeamFranceTriple
{
    public partial class ShoutboxList : BaseUserControl
    {
        private HiddenField author;
        private Label authorLabel, msg, CreationDateShoutBoxLabel;
        private Button InsertButtonInsertItem, CancelButtonInsertItem, btn, btn2;
        private TextBox MessageShoutBoxTextBoxInsertItem, MessageShoutBoxEditTextBox;
        private FranceTriple.Dal.ShoutBox ThisMessage;

        protected void Page_Load(object sender, EventArgs e)
        {
            ShowSmiliesLiteral.Text = ShoutboxDal.GetSmilies("DoSmilieInList");
        }
        protected void ShoutBoxListView_DataBound(object sender, EventArgs e)
        {
            author = ShoutBoxListView.InsertItem.FindControl("UserNameAuthorShoutBoxHidden") as HiddenField;
            authorLabel = ShoutBoxListView.InsertItem.FindControl("UserNameAuthorShoutBoxLabel") as Label;

            InsertButtonInsertItem = ShoutBoxListView.InsertItem.FindControl("InsertButton") as Button;
            CancelButtonInsertItem = ShoutBoxListView.InsertItem.FindControl("CancelButton") as Button;
            MessageShoutBoxTextBoxInsertItem = ShoutBoxListView.InsertItem.FindControl("MessageShoutBoxTextBox") as TextBox;

            if (Session["AnonymousID"] == null)
            {
                author.Value = ThisMaster.Requester.Profile.UserName;
                authorLabel.Text = ThisMaster.Requester.Profile.UserName;

                InsertButtonInsertItem.Visible = true;
                CancelButtonInsertItem.Visible = true;
                MessageShoutBoxTextBoxInsertItem.Visible = true;
                ShowSmiliesLiteral.Visible = true;
            }
            else
            {
                InsertButtonInsertItem.Visible = false;
                CancelButtonInsertItem.Visible = false;
                MessageShoutBoxTextBoxInsertItem.Visible = false;
                ShowSmiliesLiteral.Visible = false;
            }
        }
        protected void ShoutBoxListView_ItemEditing(object sender, ListViewEditEventArgs e)
        {

        }
        protected void ShoutBoxListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ThisMessage = e.Item.DataItem as FranceTriple.Dal.ShoutBox;

                btn = e.Item.FindControl("DeleteButton") as Button;
                btn2 = e.Item.FindControl("EditButton") as Button;

                if (ThisMaster.Requester.Profile != null && (ThisMessage.UserNameAuthorShoutBox.ToLower() == ThisMaster.Requester.Profile.UserName.ToLower() || ThisMaster.Requester.Roles.Contains("RoleAdminForum")))
                {
                    if (btn != null)
                        btn.Visible = true;
                    if (btn2 != null)
                        btn2.Visible = true;
                }

                msg = e.Item.FindControl("MessageShoutBoxLabel") as Label;
                if (msg != null)
                    msg.Text = ShoutboxDal.ReplaceSmiliesTextToImage(msg.Text);

                MessageShoutBoxEditTextBox = e.Item.FindControl("MessageShoutBoxEditTextBox") as TextBox;
                if (MessageShoutBoxEditTextBox != null)
                    MessageShoutBoxEditTextBox.Text = MessageShoutBoxEditTextBox.Text.HtmlToText();


                CreationDateShoutBoxLabel = e.Item.FindControl("CreationDateShoutBoxLabel") as Label;
                if (CreationDateShoutBoxLabel != null)
                    CreationDateShoutBoxLabel.Text = ThisMessage.CreationDateShoutBox.ToUserTimeZone(ThisMaster.Requester.Profile.TimeZone).ToString();
            }
        }
        protected void ShoutBoxListView_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
            e.Values["MessageShoutBox"] = e.Values["MessageShoutBox"].ToString().TextToHtml();
        }
        protected void ShoutBoxListView_ItemInserted(object sender, ListViewInsertedEventArgs e)
        {
            //RefreshMasterShoutbox();
        }
        protected void ShoutBoxListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
            e.NewValues["MessageShoutBox"] = e.NewValues["MessageShoutBox"].ToString().TextToHtml();
        }
        protected void ShoutBoxListView_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {
            //RefreshMasterShoutbox();
        }
        protected void ShoutBoxListView_ItemDeleted(object sender, ListViewDeletedEventArgs e)
        {
            //RefreshMasterShoutbox();
        }
        private void RefreshMasterShoutbox()
        {
            ShoutBox sb;
            UpdatePanel up;
            ListView lv;

            sb = ThisMaster.FindControl("ShoutBox1") as ShoutBox;
            if (sb != null)
            {
                up = sb.FindControl("ShoutboxUpdatePanel") as UpdatePanel;
                if (up != null)
                {
                    lv = sb.FindControl("ShoutboxMessageListView") as ListView;
                    if (lv != null)
                    {
                        lv.DataBind();
                        up.Update();
                    }
                }
            }
        }
    }
}
